﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace eLearning.Helpers
{
	public static class Helper
	{
		public static void SerializeObject(string filename, System.Type oType, object objectToSerialize)
		{
			TextWriter textWriter = null;
			try
			{
				XmlSerializer serializer = new XmlSerializer(oType);
				textWriter = new StreamWriter(filename);
				serializer.Serialize(textWriter, objectToSerialize);
			}
			finally
			{
				if (textWriter != null)
					textWriter.Close();
			}
		}

		public static object DeSerializeObject(string filename, System.Type oType)
		{
			TextReader textReader = null;
			object oBatch = null;
			try
			{
				XmlSerializer deserializer = new XmlSerializer(oType);
				textReader = new StreamReader(filename);

				oBatch = deserializer.Deserialize(textReader);
			}
			finally
			{
				if (textReader != null)
					textReader.Close();
			}

			return oBatch;
		}

		public static string SerializeObjectToString(System.Type oType, object objectToSerialize)
		{
			TextWriter textWriter = null;
			string result = null;
			try
			{
				XmlSerializer serializer = new XmlSerializer(oType);
				MemoryStream oMemoryStream = new MemoryStream();
				textWriter = new StreamWriter(oMemoryStream);
				serializer.Serialize(textWriter, objectToSerialize);

				result = System.Text.Encoding.UTF8.GetString(oMemoryStream.ToArray());
			}
			finally
			{
				if (textWriter != null)
					textWriter.Close();
			}

			return result;
		}

		public static object DeSerializeObjectFromString(string value, System.Type oType)
		{
			TextReader textReader = null;
			object oBatch = null;
			try
			{
				XmlSerializer deserializer = new XmlSerializer(oType);
				textReader = new StringReader(value);

				oBatch = deserializer.Deserialize(textReader);
			}
			finally
			{
				if (textReader != null)
					textReader.Close();
			}

			return oBatch;
		}

		public static object DeSerializeObject(byte[] arrByte, System.Type oType)
		{
			object oReturn = null;
			using (MemoryStream stream = new MemoryStream(arrByte))
			{
				XmlSerializer serializer = new XmlSerializer(oType);
				oReturn = serializer.Deserialize(stream);
				//stream.Close();
			}

			return oReturn;
		}

		public static object DeserializeFromXmlDoc(XmlDocument xml, Type type)
		{
			XmlReader reader = null;
			object result = null;

			try
			{
				XmlSerializer s = new XmlSerializer(type);
				byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xml.OuterXml);

				MemoryStream ms = new MemoryStream(buffer);
				reader = new XmlTextReader(ms);

				result = s.Deserialize(reader);
			}
			finally
			{
				if (reader != null) reader.Close();
			}

			return result;
		}

		public static bool ValidateUser(string username, string password)
		{
			return true;
		}
	}
}
